import {
  Column,
  Entity,
  JoinColumn,
  ManyToMany,
  OneToMany,
  Tree,
  TreeChildren,
  TreeParent,
} from 'typeorm'
import { TMenuStatus } from '@/enum/commom.emun'
import { Operate } from './operate.entity'
import { Permission } from './permission.entity'
import { Role } from './role.entity'

@Entity()
@Tree('closure-table')
export class Menu extends Operate {
  @Column({ name: 'parent_id', nullable: true }) // 外键属性，指向父类别的ID
  parentId: string

  @TreeParent({
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'parent_id' }) // 指定外键列
  parent: Menu

  @TreeChildren()
  children: Menu[]

  @Column({
    name: 'menu_type',
    type: 'varchar',
    length: 1,
    comment: '菜单类型1：目录；2：菜单',
  })
  menuType: string

  @Column({ name: 'status', type: 'varchar', length: 1, comment: '状态；1:可用，2:禁用' })
  status: TMenuStatus

  @Column({ name: 'menu_name', type: 'varchar', length: 255, comment: '菜单名称' })
  menuName: string

  @Column({ name: 'route_name', type: 'varchar', length: 255, comment: '路由名称' })
  routeName: string

  @Column({ name: 'route_path', type: 'varchar', length: 255, nullable: true, comment: '菜单路由' })
  routePath: string | null

  @Column({ name: 'component', type: 'varchar', length: 40, nullable: true, comment: '菜单组件' })
  component: string | null

  @Column({ name: 'icon', type: 'varchar', length: 40, nullable: true, comment: '菜单图标' })
  icon: string | null

  @Column({
    name: 'icon_type',
    type: 'varchar',
    length: 1,
    nullable: true,
    comment: '图标类型 1: iconify图标 2 本地图标',
  })
  iconType: string | null

  @Column({ name: 'order', type: 'int', nullable: true, comment: '排序' })
  order: number

  @Column({ name: 'i18n_key', type: 'varchar', length: 255, nullable: true, comment: 'i18n字符' })
  i18nKey: string | null

  @Column({ name: 'href', type: 'varchar', length: 255, nullable: true, comment: '是否外链' })
  href: string | null

  @Column({ name: 'keep_alive', type: 'boolean', default: false, comment: '是否缓存' })
  keepAlive: boolean

  @Column({ name: 'constant', type: 'boolean', default: false, comment: '是否常量路由' })
  constant: boolean

  @Column({ name: 'hide_in_menu', type: 'boolean', default: false, comment: '是否隐藏菜单' })
  hideInMenu: boolean

  @Column({ name: 'multi_tab', type: 'boolean', default: false, comment: '是否支持多页签' })
  multiTab: boolean

  @Column({
    name: 'fixed_index_in_tab',
    type: 'int',
    nullable: true,
    comment: '固定在页签中的序号',
  })
  fixedIndexInTab: number

  @Column({ name: 'query', type: 'simple-json', nullable: true, comment: '路由参数' })
  query: { key: string; value: string }[]

  @Column({ name: 'active_menu', nullable: true, comment: '高亮菜单' })
  activeMenu: string | null

  @ManyToMany(() => Role, (role) => role.menus, {
    cascade: true,
  })
  roles: Role[]

  @OneToMany(() => Permission, (permission) => permission.menu)
  permissions: Permission[]
}
